package cn.sjlink.store.service.impl;

import cn.sjlink.config.BusinessException;
import cn.sjlink.store.dao.StoreShopDeviceMapper;
import cn.sjlink.store.dao.StoreShopSiteMapper;
import cn.sjlink.store.dao.YoshopOperateMapper;
import cn.sjlink.store.entity.StoreShopDevice;
import cn.sjlink.store.entity.StoreShopSite;
import cn.sjlink.store.entity.YoshopOperate;
import cn.sjlink.store.form.LoginForm;
import cn.sjlink.store.service.ShopSiteService;
import cn.sjlink.util.*;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopSiteServiceImpl implements ShopSiteService {

    @Autowired
    private YoshopOperateMapper yoshopOperateMapper;
    @Autowired
    private StoreShopSiteMapper shopSiteMapper;
    @Autowired
    private StoreShopDeviceMapper shopDeviceMapper;

    @Override
    public Result banner(String stackNo) {
        if(StringUtils.isNotEmpty(stackNo)){
            Map result = new HashMap();
            String nowDateFormat = DateUtil.getNowDateFormat();
            List<YoshopOperate> list = yoshopOperateMapper.findList(nowDateFormat);
            StoreShopSite shopSite = shopSiteMapper.findByDeviceNo(stackNo);
            result.put("banner",list);
            result.put("siteName",shopSite.getSiteName());
            result.put("appBarcode",shopSite.getAppBarcode());
            return new Result(result);
        }
        return new Result(false,"参数错误");
    }

    @Override
    public Result login(LoginForm loginForm) throws Exception {
        if(StringUtils.isEmpty(loginForm.getPassword()) || StringUtils.isEmpty(loginForm.getLoginName())){
            return new Result(false,"用户名/密码错误");
        }
        StoreShopDevice storeShopDevice = shopDeviceMapper.findByLoginName(loginForm.getLoginName(),loginForm.getStackNo());
        if(storeShopDevice!=null){
            String encryptedPassword = EncryptUtil.getEncryptedPassword(loginForm.getPassword().trim(), storeShopDevice.getSalt());
            if(storeShopDevice.getPassword().equals(encryptedPassword)){
                return new Result(true,"登录成功");
            }
        }
        return new Result(false,"用户名/密码错误");
    }

    @Override
    public Result getBarcode(String stackNo)throws Exception {
        StoreShopDevice deviceNo = shopDeviceMapper.findByDeviceNo(stackNo);
        if(deviceNo==null){
            return new Result(false,"设备不存在");
        }
        if(StringUtils.isEmpty(deviceNo.getBarcode())){
            String barcode_url = ConfigUtil.get("barcode_url");
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            BitMatrix bitMatrix = qrCodeWriter.encode(barcode_url+"stockId="+stackNo+"&type=stack", BarcodeFormat.QR_CODE, 350, 350);
            BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "png", bs);
            //最终byte数组
            byte[] bytes = bs.toByteArray();
            UploadClietUtil clietUtil = new UploadClietUtil();
            InputStream in = new ByteArrayInputStream(bytes);
            String s = clietUtil.uploadFile2OSS(in,DateUtil.getDefaultDate() + ".png");
            String[] split = s.split("\\?");
            String imgUrl = split[0];
            deviceNo.setBarcode(imgUrl);
            shopDeviceMapper.updateByPrimaryKeySelective(deviceNo);
            return new Result(true,"获取成功",imgUrl);
        }else{
            return new Result(true,"获取成功",deviceNo.getBarcode());
        }

    }

    @Override
    public Result appBarcode(String stackNo) {
        StoreShopSite storeShopSite = shopSiteMapper.findByDeviceNo(stackNo);
        return new Result(true,"获取成功",storeShopSite.getAppBarcode());
    }
}
